
import { _decorator, Button, Component, Label, Node, Sprite } from 'cc';
import { AppControll, EventName, MainGun_Item } from '../manager/AppControll';
import { DataHelper } from '../system/DataHelper';
import { GameData } from '../system/GameData';
import EvtManger from '../tools/EvtManger';
import { SoundManager } from '../manager/SoundManger';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = MenuItem
 * DateTime = Mon May 15 2023 15:16:06 GMT+0800 (GMT+08:00)
 * Author = guoxun
 * FileBasename = MenuItem.ts
 * FileBasenameNoExtension = MenuItem
 * URL = db://assets/script/fight/MenuItem.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('MenuItem')
export class MenuItem extends Component {
    private mNdText: Node = null;
    private mNdButtonList: Node = null;
    private mNdLevelUp: Node = null;
    private mNdCostAmount: Node = null;
    private mNdconditions: Node = null;
    private mConfig: MainGun_Item = null;
    private mStrType: string = null;
    private mLevel: number = 0;
    private mNdGold: Node = null;

    protected onLoad(): void {
        this.mNdText = this.node.getChildByName("levelAttr");
        this.mNdButtonList = this.node.getChildByName("buttonList");
        this.mNdLevelUp = this.mNdButtonList.getChildByName("levelup_btn");
        this.mNdCostAmount = this.mNdButtonList.getChildByName("spend");
        this.mNdconditions = this.mNdButtonList.getChildByName("unseal");
        this.mNdGold = this.mNdButtonList.getChildByName("btn_icon_gold");
        this.mNdLevelUp.on(Node.EventType.TOUCH_END, this.LevelUp, this);
        this.RegisteredEvent();
    }

    RegisteredEvent() {
        EvtManger.on(EventName.monsterDeath, this, this.InitLock);
    }

    protected onDestroy(): void {
        EvtManger.off(EventName.monsterDeath, this, this.InitLock);
    }

    LevelUp() {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect("ui/click");
        if (this.GetNextLevel() && this.mNdLevelUp.getComponent(Button).interactable) {
            GameData.AddGold(this.mConfig.UpgradeConsumption * -1);
            GameData.playerData[this.mStrType] += 1;
            EvtManger.do(EventName.goldUpdate);
            EvtManger.do(EventName.mainGunLevelUp);
            this.InitItemByConfig(this.mStrType, this.GetNextLevel());
            SoundManager.Instance<SoundManager>().PlayEffect("ui/levelup");
        } else {
            return;
        }

    }

    InitItemByConfig(strType: string, config: MainGun_Item) {
        this.mConfig = config;
        this.mStrType = strType;
        this.mLevel = GameData.playerData[strType];
        let curLevel: Node = this.mNdText.getChildByName("now");
        let nextLevel: Node = this.mNdText.getChildByName("next");
        let nextConfig = this.GetNextLevel();

        let tempStr = "";
        if (this.mStrType == "MGCriticalHitRateLv" || this.mStrType == "MGCriticalHitDamageLv" || this.mStrType == "MGDamageBoostLv") {
            tempStr = "%";
        }

        curLevel.getChildByName("attr_level").getComponent(Label).string = this.mLevel + "";
        curLevel.getChildByName("attrNum").getComponent(Label).string = config.attributeNum + tempStr;
        nextLevel.getChildByName("attr_level").getComponent(Label).string = nextConfig ? (this.mLevel + 1) + "" : "-";
        nextLevel.getChildByName("attrNum").getComponent(Label).string = nextConfig ? nextConfig.attributeNum + tempStr : "-";
        this.mNdCostAmount.getComponent(Label).string = config.UpgradeConsumption + "";
        this.mNdconditions.getComponent(Label).string = "荣誉值达到" + this.mConfig.upgradation + "解锁";
        this.InitLock();
    }

    GetNextLevel() {
        let result = null;
        switch (this.mStrType) {
            case "MGAttackLv":
                result = DataHelper.GetAttackByLv(this.mLevel + 1);
                break;
            case "MGCriticalHitRateLv":
                result = DataHelper.GetCriticalHitRateByLv(this.mLevel + 1);
                break;
            case "MGCriticalHitDamageLv":
                result = DataHelper.GetCriticalHitDamageByLv(this.mLevel + 1)
                break;
            case "MGDamageBoostLv":
                result = DataHelper.GetDamageBoostByLv(this.mLevel + 1)
                break
        }
        return result;
    }

    InitLock() {
        let a = () => {
            this.mNdCostAmount.active = true;
            this.mNdconditions.active = false;
            this.mNdGold.active = true;
            if (GameData.playerData.Gold >= this.mConfig.UpgradeConsumption) {
                if (GameData.playerData.mainGuide && this.mConfig && this.mLevel <= 1) {
                    GameData.playerData.mainGuide = false;
                    EvtManger.do(EventName.mainGuide);
                }
                this.mNdLevelUp.getComponent(Sprite).grayscale = false;
                this.mNdLevelUp.getComponent(Button).interactable = true;
            } else {
                this.mNdLevelUp.getComponent(Sprite).grayscale = true;
                this.mNdLevelUp.getComponent(Button).interactable = false;
            }
        }
        if (this.mConfig.upgradation == null) {
            a();
        } else {
            if (GameData.playerData.Honor >= this.mConfig.upgradation) {
                a();
            } else {
                this.mNdCostAmount.active = false;
                this.mNdGold.active = false;
                this.mNdconditions.active = true;
                this.mNdLevelUp.getComponent(Sprite).grayscale = true;
                this.mNdLevelUp.getComponent(Button).interactable = false;
            }
        }
        if (!this.GetNextLevel()) {
            this.mNdGold.active = false;
            this.mNdCostAmount.active = false;
            this.mNdconditions.active = true;
            this.mNdconditions.getComponent(Label).string = "已经满级"
            this.mNdLevelUp.getComponent(Sprite).grayscale = true;
            this.mNdLevelUp.getComponent(Button).interactable = false;
        }
    }

}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
